package edu.wit.system.controller;


import edu.wit.common.api.domain.SysLogininfor;
import edu.wit.common.constant.Constants;
import edu.wit.common.core.web.BaseController;
import edu.wit.common.core.web.page.TableDataInfo;
import edu.wit.common.result.R;
import edu.wit.common.utils.ServletUtils;
import edu.wit.common.utils.ip.AddressUtils;
import edu.wit.common.utils.ip.IpUtils;
import edu.wit.common.utils.poi.ExcelUtil;
import edu.wit.common.security.config.TokenManager;
import edu.wit.system.service.SysLogininforService;
import eu.bitwalker.useragentutils.UserAgent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 系统访问记录
 *
 */
@RestController
@RequestMapping("/sys/logininfor")
@Api(tags = "登陆日志")
public class SysLogininforController extends BaseController {
    @Autowired
    private SysLogininforService logininforService;

    @Autowired
    private TokenManager manager;

//    @PreAuthorize(hasPermi = "system:logininfor:list")
    @ApiOperation(value = "查询登录日志")
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor logininfor) {
        startPage();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @ApiOperation(value = "根据用户名查询登录日志")
    @GetMapping("/listByCurrentUser")
    public TableDataInfo listByCurrentUser(SysLogininfor logininfor) {
        startPage();
        logininfor.setUserName(manager.getUserFromToken(ServletUtils.getRequest().getHeader("token")));
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

//    @Log(title = "导出登录日志", businessType = BusinessType.EXPORT)
//    @PreAuthorize(hasPermi = "system:logininfor:export")
    @PostMapping("/export")
    @ApiOperation(value = "导出登录日志")
    public void export(HttpServletResponse response, SysLogininfor logininfor) throws IOException {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

//    @PreAuthorize(hasPermi = "system:logininfor:remove")
    //@Log(title = "删除登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    @ApiOperation(value = "删除登录日志")
    public R remove(@PathVariable Long[] infoIds) {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

//    @PreAuthorize(hasPermi = "system:logininfor:remove")
   // @Log(title = "清理登录日志", businessType = BusinessType.DELETE)
    @ApiOperation(value = "清理登录日志")
    @DeleteMapping("/clean")
    public R clean() {
        logininforService.cleanLogininfor();
        return R.ok();
    }

//    @Log(title = "保存登录日志", businessType = BusinessType.DELETE)
    @ApiOperation(value = "保存登录日志")
    @PostMapping("/savelog")
    public R saveLogininfor(@RequestParam("username") String username, @RequestParam("status") String status,
            @RequestParam("message") String message) {
        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("user-agent"));

        // 封装对象
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName(username);
        logininfor.setIpaddr(ip);
        logininfor.setMsg(message);

        logininfor.setAddress(AddressUtils.getRealAddressByIP(ip));
        logininfor.setBrowser(userAgent.getBrowser().toString());
        logininfor.setOs(userAgent.getOperatingSystem().toString());

        logininfor.setAccessTime(new Date());

        // 日志状态
        if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status)) {
            logininfor.setStatus("0");
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            logininfor.setStatus("1");
        }
        return toAjax(logininforService.insertLogininfor(logininfor));
    }

    @ApiOperation(value = "保存登录日志")
    @PostMapping("/saveLogin")
    public R saveLogin(@RequestBody SysLogininfor sysLogininfor) {
        return toAjax(logininforService.insertLogininfor(sysLogininfor));
    }
}
